#!/bin/bash
###############################
# A Script, that starts the 'cyclictest' from Thomas Gleixner's 'rt-tests' package with different
# configurations.
# This scripts also does formatting in a way, that it is possible to read the data with 'R' or other 
# programs that can process 'Comma Separated Values (csv)'-files
###############################
# V 1.0, 2010
# Rene Dohl, Pavlos Giannakis and Robin Mueller-Bady
# Fachhochschule Frankfurt am Main
# University of Applied Sciences
# Master 'High Integrity Systems'
###############################

# Check for 1 argument else print a help message
if [ $# -ne 1 ] 
then
    echo "Usage: $0 [filename]"
    exit
fi

# Create a directory for this test
mkdir $1
chmod ugo+rwx $1
cd $1

# Set environment variables for starting the program in a uniform way
CYCLICTIME=2m
CLOCK=0
TESTCASE_1=$1'.task25'.csv
TESTCASE_2=$1'.task100'.csv
TESTCASE_3=$1'.task25.prio'.csv
TESTCASE_4=$1'.task100.prio'.csv

# Start the testcases
echo "Starting Testcase 1..."
cyclictest -t$TASKNUMBER1 -o10 -D$CYCLICTIME -m -v -c$CLOCK > $TESTCASE_1
echo "Starting Testcase 2..."
cyclictest -t$TASKNUMBER2 -o10 -p80 -D$CYCLICTIME -m -v -c$CLOCK > $TESTCASE_2
echo "Starting Testcase 3..."
cyclictest -t$TASKNUMBER1 -o10 -D$CYCLICTIME -m -v -c$CLOCK > $TESTCASE_3
echo "Starting Testcase 4..."
cyclictest -t$TASKNUMBER2 -o10 -D$CYCLICTIME -m -p80 -v -c$CLOCK > $TESTCASE_4

# Set the user rights for the files and directories
chmod ugo+rwx $1*

# Delete the first lines of overhead.
echo "Deleting Header of Testcase 1..."
sed -i -e '1,25d' $TESTCASE_1
echo "Deleting Header of Testcase 2..."
sed -i -e '1,100d' $TESTCASE_2
echo "Deleting Header of Testcase 3..."
sed -i -e '1,25d' $TESTCASE_3
echo "Deleting Header of Testcase 4..."
sed -i -e '1,100d' $TESTCASE_4

# Delete unnecessary blanks to let R or OpenOffice be able to parse them correctly
echo "Deleting spaces in Testcase 1..."
sed -i -e "s/[ <tab>]*//g" $TESTCASE_1
echo "Deleting spaces in Testcase 2..."
sed -i -e "s/[ <tab>]*//g" $TESTCASE_2
echo "Deleting spaces in Testcase 3..."
sed -i -e "s/[ <tab>]*//g" $TESTCASE_3
echo "Deleting spaces in Testcase 4..."
sed -i -e "s/[ <tab>]*//g" $TESTCASE_4

# Use blanks as delimiter instead of ':'
echo "Replacing spaces in Testcase 1..."
sed -i -e 's/:/ /g' $TESTCASE_1
echo "Replacing spaces in Testcase 2..."
sed -i -e 's/:/ /g' $TESTCASE_2
echo "Replacing spaces in Testcase 3..."
sed -i -e 's/:/ /g' $TESTCASE_3
echo "Replacing spaces in Testcase 4..."
sed -i -e 's/:/ /g' $TESTCASE_4

# Add a header as first line describing the columns
echo "Adding Header to Testcase 1..."
sed -i -e '1{x;p;x}' $TESTCASE_1
sed -i -e 's/^$/Task Count Latency/' $TESTCASE_1
echo "Adding Header to Testcase 2..."
sed -i -e '1{x;p;x}' $TESTCASE_2
sed -i -e 's/^$/Task Count Latency/' $TESTCASE_2
echo "Adding Header to Testcase 3..."
sed -i -e '1{x;p;x}' $TESTCASE_3
sed -i -e 's/^$/Task Count Latency/' $TESTCASE_3
echo "Adding Header to Testcase 4..."
sed -i -e '1{x;p;x}' $TESTCASE_4
sed -i -e 's/^$/Task Count Latency/' $TESTCASE_4
